package com.film.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.film.domain.Review;
import com.film.service.ReviewPriaseService;
import com.film.service.ReviewService;
import com.film.service.impl.ReviewPriaseServiceImpl;
import com.film.service.impl.ReviewServiceImpl;

/**
 * 该控制类用来做电影评价
 * @author 2119196781
 *
 */
@WebServlet("/review.do")
public class FilmReviewController extends HttpServlet{
	
	private ReviewService reviewService = new ReviewServiceImpl();
	private  ReviewPriaseService  reviewPriaseService  = new ReviewPriaseServiceImpl();
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		request.setCharacterEncoding("utf-8");
		String uId = request.getParameter("uId");
		String filmId = request.getParameter("filmId");
		String content = request.getParameter("content");
		String score = request.getParameter("score");
		//通过电影的id和用户的id查询 看用户是否重复
		Review review = reviewService.selectReviewInfo(Integer.parseInt(filmId), Integer.parseInt(uId));
		if(review == null ) {
			//评论的添加
			int result = reviewService.insertReview(Integer.parseInt(uId), Integer.parseInt(filmId), content, Float.parseFloat(score));
		}else {
			int result = reviewService.updateReview(Integer.parseInt(uId), Integer.parseInt(filmId), content, Float.parseFloat(score));
		}
		List<Integer> revIds = reviewPriaseService.selectUserReviewPriase(Integer.parseInt(uId));
		session.setAttribute("revIds", revIds);
			
		//查询所有的评论信息
		List<Review> reviews = reviewService.selectAllReview(Integer.parseInt(filmId));
		//通过电影的id和用户的id查询 看用户是否重复
		Review reviewInfo = reviewService.selectReviewInfo(Integer.parseInt(filmId), Integer.parseInt(uId));
		session.setAttribute("reviewInfo", reviewInfo);
		session.setAttribute("reviews",reviews);
		response.sendRedirect("selectOneFilm.do?filmId="+filmId);
	}
}
